<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48" @keyup.enter.native="searchList">
            <a-col :xxl="6" :lg="8" :md="12" :sm="24">
              <a-form-item label="会员标签名称">
                <a-input v-model="queryParam.memberName" placeholder="" allowClear />
              </a-form-item>
            </a-col>
            <a-col :xxl="6" :lg="8" :md="12" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.status" placeholder="请选择" :getPopupContainer="triggerNode => triggerNode.parentNode">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option :value="0">禁用</a-select-option>
                  <a-select-option :value="1">启用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xxl="12" :lg="8" :md="24" :sm="24">
              <span class="table-page-search-submitButtons" :style="{ float: 'right', overflow: 'hidden' }">
                <a-button type="primary" @click="searchList">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetList">重置</a-button>
                <a-button style="margin-left: 8px" type="primary" icon="plus" @click="handleEditModal">新建标签</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table ref="table" size="default" :rowKey="record => record.id" :columns="columns" :data="loadData" :pagination="paginationProps">
        <span slot="status" slot-scope="text">
          <a-badge status="success" text="启用" v-if="text == 1" />
          <a-badge status="error" text="禁用" v-else />
        </span>
        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定启用吗?" ok-text="Yes" cancel-text="No" @confirm="handleEnable(record, 1)" v-if="record.status == 0">
            <a href="#">启用</a>
          </a-popconfirm>
          <a-popconfirm title="确定禁用吗?" ok-text="Yes" cancel-text="No" @confirm="handleEnable(record, 0)" v-if="record.status == 1">
            <a href="#">禁用</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" ok-text="Yes" cancel-text="No" @confirm="handleDelete(record)">
            <a href="#">删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </a-card>
    <AddModal ref="modalForm" @updateTable="refreshList"></AddModal>
    <a-modal :visible="visible" :destroyOnClose="true" title="新建" :footer="false" @cancel="visible = false">
      <div style="width:100%">
        <label for="" style="margin-right:12px">请选择创建方式：</label>
        <a-select v-model="addType" placeholder="请选择" :getPopupContainer="triggerNode => triggerNode.parentNode" style="width:200px">
          <a-select-option :value="1">导入</a-select-option>
        </a-select>
      </div>
      <div class="btn">
        <a-button style="margin-right:12px" @click="visible = false">取消</a-button>
        <a-button type="primary" @click="handleAdd">确定</a-button>
      </div>
    </a-modal>
  </div>
</template>
<script>
import { listMixin } from '@/views/box/util/listMixin'
import AddModal from './addModal'
import { marketingMemberList, marketingMemberEnable, marketingMemberDelete } from '@/api/box/member'
let columns = [
  {
    title: 'ID',
    align: 'center',
    dataIndex: 'id'
  },
  { title: '会员标签名称', dataIndex: 'memberName', align: 'center' },
  { title: '创建时间', dataIndex: 'createTime', align: 'center' },
  { title: '状态', dataIndex: 'status', align: 'center', scopedSlots: { customRender: 'status' } },
  { title: '操作', fixed: 'right', align: 'center', scopedSlots: { customRender: 'action' } }
]
export default {
  mixins: [listMixin],
  components: { AddModal },
  data() {
    return {
      columns,
      queryParam: {
        status: ''
      },
      loadData: parameter => this.fetchData(parameter),
      visible: false,
      addType: 1
    }
  },
  methods: {
    fetchData(parameter) {
      let params = {
        page: parameter,
        requestData: this.queryParam
      }
      try {
        return this.fetchList(params, marketingMemberList).then(res => {
          if (res.code == 0) {
            return {
              data: res.data,
              page: res.page
            }
          } else {
            return {
              data: [],
              page: {
                pageNo: 1,
                pageSize: 10,
                count: 0
              }
            }
          }
        })
      } catch (err) {
        console.log(err)
      }
    },
    resetList() {
      this.queryParam = {
        status: ''
      }
      this.$refs.table.refresh(true)
    },
    handleEnable(record, type) {
      let params = {
        requestData: {
          id: record.id,
          status: type
        }
      }
      marketingMemberEnable(params).then(res => {
        if (res.code === 0) {
          this.$message.success(res.msg)
          this.$refs.table.refresh()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleDelete(record) {
      let params = {
        requestData: {
          id: record.id
        }
      }
      marketingMemberDelete(params).then(res => {
        if (res.code === 0) {
          this.$message.success(res.msg)
          this.$refs.table.refresh()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleEditModal() {
      this.visible = true
    },
    handleAdd() {
      this.visible = false
      this.$refs.modalForm.show()
    }
  }
}
</script>
<style lang="less" scoped>
.btn {
  width: 100%;
  text-align: center;
  margin-top: 24px;
}
</style>

